package com.dao;
/***
*test
*/
import com.util.*;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
public class TestDao {
	public TestDao(){
	
	}
	public int addTest(HttpServletRequest req){
		String name=req.getParameter("name")==null?"":req.getParameter("name");
		String sql ="insert into TBL_TEST("+
        		"NAME"+
        	") values("+
        		"'"+name+"'"+
        	")";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public int delTest(HttpServletRequest req){
         String id=req.getParameter("id");
		String sql ="delete from TBL_TEST where 1=1 "+
    		" and id ='"+id+"'"+
    	"";
    	DB db = new DB();
    	return db.executeSql(sql);	
	}
	public int updTest(HttpServletRequest req){
		String name=req.getParameter("name")==null?"":req.getParameter("name");
        String id=req.getParameter("id");
		String sql ="update TBL_TEST set "+
        		"NAME='"+name+"'"+
        	" where 1=1 "+
    		" and id ='"+id+"'"+
    	"";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public Map<String,Object> findTestById(HttpServletRequest req){
         String id=req.getParameter("id");
		String sql ="select * from TBL_TEST where 1=1 "+
    		" and id ='"+id+"'"+
    	"";
    	DB db = new DB();
    	Map<String,Object> map = null;
    	List<Map<String,Object>> list = db.query(sql);
    	if(list!=null&&list.size()>0){
    		map = list.get(0);
    	}
    	return map;	
	}
	
	
	public PageModel findTestByCondition(HttpServletRequest req){
		String name=req.getParameter("name")==null?"":req.getParameter("name");
		req.setAttribute("name", name);
         String id=req.getParameter("id");
		String sql ="";
		String where="";
		if(name!=null&&!"".equals(name)){
    		where+=" and NAME like '%"+name+"%'";
		}
    	String countsql ="select count(*) from TBL_TEST where 1=1 "+where;
    	PageModel model = new PageModel();
		String tempPage = req.getParameter("currentPage");
		int currentPage = 1;
		try {
			currentPage = Integer.parseInt(tempPage);
		} catch (NumberFormatException e) {
			currentPage=1;
		}
		model.setCurrentPage(currentPage);
		String tempCount = req.getParameter("pageCount");
		int pageCount = 20;
		try {
			pageCount = Integer.parseInt(tempCount);
		} catch (NumberFormatException e) {
			pageCount=20;
		}
		model.setPageCount(pageCount);
		sql="select * from (select t.*,rownum rn from (select * from TBL_TEST where 1=1 "+where+" order by  id  desc) t where rownum<="+((currentPage)*pageCount)+") where rn >="+((currentPage-1)*pageCount+1);
    	DB db = new DB();
    	int count = Integer.parseInt(db.queryFirst(countsql));
    	model.setTotalCount(count);
    	if(count>0){
    		model.setRes(db.query(sql));
    	}
    	return model;	
	}
	
}